package net.thxjj.adminServer.mongodao;

import net.thxjj.adminServer.entity.aep.SmokeData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class MongoSmokeDao {
    @Autowired
    private MongoTemplate mongoTemplate;

    //导入所有数据
    public void saveAllData(List<SmokeData> list){
        mongoTemplate.insert(list,SmokeData.class);
    }

    private void updateSmokeData(SmokeData smokeData) {
        Query query = new Query(Criteria.where("deviceId").is(smokeData.getDeviceId()).and("timestamp").is(smokeData.getTimestamp()));
        Update update = new Update()
                .set("tx_Power",smokeData.getTx_Power())
                .set("snr",smokeData.getSnr())
                .set("smoke_value",smokeData.getSmoke_value())
                .set("smoke_state",smokeData.getSmoke_state())
                .set("signalPower",smokeData.getSignalPower())
                .set("cell_ID",smokeData.getCell_ID())
                .set("battery_value",smokeData.getBattery_value());

        mongoTemplate.upsert(query,update, SmokeData.class);
    }

    private SmokeData findSmokeDataByDeviceIdAndTime(String deviceId, long time){
        Query query = new Query(Criteria.where("deviceId").is(deviceId).and("timestamp").is(time));
        return mongoTemplate.findOne(query,SmokeData.class);
    }

    //导入校验（如果有timestamp和deviceId重合则更新）
    public void saveSmokeDatas(List<SmokeData> list){
        for (SmokeData smokeData:
                list) {
            SmokeData po = findSmokeDataByDeviceIdAndTime(smokeData.getDeviceId(),smokeData.getTimestamp());
            if(po==null){
                mongoTemplate.save(smokeData);
            }else{
                updateSmokeData(smokeData);
            }
        }
    }

    public void saveSmokeData(SmokeData smokeData){
        SmokeData po = findSmokeDataByDeviceIdAndTime(smokeData.getDeviceId(),smokeData.getTimestamp());
        if(po==null){
            mongoTemplate.save(smokeData);
        }else{
            updateSmokeData(smokeData);
        }
    }

    public SmokeData getNewestSmokeAlarm(String device_id){
        Query query = new Query(Criteria.where("deviceId").is(device_id).and("smoke_state").is(0));
        query.with(new Sort(Sort.Direction.DESC,"timestamp"));
        return mongoTemplate.findOne(query, SmokeData.class);
    }

    public SmokeData getNewestSmokeAlarmByTime(String device_id,long time){
        Query query = new Query(Criteria.where("deviceId").is(device_id).and("smoke_state").is(0).and("timestamp").lt(time));
        query.with(new Sort(Sort.Direction.DESC,"timestamp"));
        return mongoTemplate.findOne(query, SmokeData.class);
    }

    public SmokeData getNewestSmokeNormal(String device_id){
        Query query = new Query(Criteria.where("deviceId").is(device_id).and("smoke_state").is(1));
        query.with(new Sort(Sort.Direction.DESC,"timestamp"));
        return mongoTemplate.findOne(query, SmokeData.class);
    }

    public SmokeData getNewestSmokeNormalByTime(String device_id,long time){
        Query query = new Query(Criteria.where("deviceId").is(device_id).and("smoke_state").is(1).and("timestamp").lt(time));
        query.with(new Sort(Sort.Direction.DESC,"timestamp"));
        return mongoTemplate.findOne(query, SmokeData.class);
    }

    public List<SmokeData> getSmokeDataAtTime(String device_id,long start_time,long end_time){
        Query query = new Query(Criteria.where("deviceId").is(device_id).andOperator(Criteria.where("timestamp").gt(start_time),Criteria.where("timestamp").lt(end_time)));
        query.with(new Sort(Sort.Direction.DESC,"timestamp"));
        return mongoTemplate.find(query, SmokeData.class);
    }

    public SmokeData getNewestSmoke(String device_id){
        Query query = new Query(Criteria.where("deviceId").is(device_id));
        query.with(new Sort(Sort.Direction.DESC,"timestamp"));
        return mongoTemplate.findOne(query, SmokeData.class);
    }

    public SmokeData getEarliestSmoke(String device_id){
        Query query = new Query(Criteria.where("deviceId").is(device_id));
        query.with(new Sort(Sort.Direction.ASC,"timestamp"));
        return mongoTemplate.findOne(query, SmokeData.class);
    }

}
